// 根据节点id查询所有的父节点id组成的数组
// 孙 -> [爷, 爹, 孙]
export const getIdsArrayByNodeId = (tree: AnyObject[], id: number) => {

  const arrList: AnyObject[] = [];
  // 将树展平，递归遍历树，将每层的节点都放在arrList中
  function walk(arr: AnyObject[]) {
    arr.forEach(item => {
      arrList.push(item);
      if (Array.isArray(item.children)) {
        walk(item.children);
      }
    });
  }
  walk(tree);

  let nodeId = id;
  const idsArray = [];
  while(nodeId) {
    idsArray.unshift(nodeId);
    const node = arrList.find(item => item.id === nodeId);
    nodeId = node?.pid;
  }

  return idsArray;
};
